<template>
  <div class="login-all">
    <div class="login">
      <div class="re-title">Two Shop登录页面</div>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 320px" round type="primary" @click="onSubmit('form')">立即登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 320px" @click="toRegister" round>还未注册？点此注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data: function() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "用户名是必须的" }],
        password: [{ required: true, message: "密码是必须的" }]
      }
    };
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$myPost(
            this,
            "/user/login",
            () => {
              this.$router.push({ path: "/" });
            },
            this.form
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toRegister: function() {
      this.$router.push({ path: "/register" });
    }
  }
};
</script>

<style scoped lang="scss">
.login-all {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  min-height: 600px;
}

.login {
  width: 500px;
  padding: 20px 50px 20px 30px;
  border-radius: 20px;
  border: 1px solid #999;
  > .re-title {
    text-align: center;
    color: #409eff;
    font-size: 30px;
    padding-bottom: 20px;
    font-weight: bold;
  }
}
</style>